angular.module('myApp')

    .controller('queryCtrl', ['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'myConstant', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                CacheService, myConstant) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //变量初始化
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {};

            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;

            /*------------------以上配置通用---------------------*/


            vm.queryDetail = queryDetail;

            vm.addItem=addItem;


            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };
            /*------------------以上方法名可选择性通用---------------------*/


            //缓存数据初始化(需要缓存的key请自定义)
            vm.cached = {
                SPECIALLY_TRANS_CODE:{}
            };

            CacheService.initCaches(vm.cached);


            function queryDetail() {
                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');

                //组织参数：查询条件 + 分页数据
                var postData = angular.copy(vm.queryBean);
                postData.pageSize = vm.pagination.pageSize;
                postData.pageNum = vm.pagination.pageNum;
                vm.pagination.queryBean = postData;
                $http.post(vm.url, vm.pagination, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination = data.object;
                            //清空选中记录
                            CheckboxService.clearChecked();
                        } else {
                            alert(data.msg);
                        }
                    });

            };


            function addItem() {
                OpenService({
                    modalTitle: '新增申请补发',
                    modalBody: 'toAdd',
                    url: 'add',
                    item: {},
                }, function (item) {
                    queryDetail();
                },'reissueApplyModalInstanceCtrl','myModalNoFooter.html');
            }






            function resetForm() {
                vm.queryBean = {};
            }

        }]);

angular.module('myApp').controller('reissueApplyModalInstanceCtrl',['$scope', '$uibModalInstance', 'modalItem', '$http',
    '$httpParamSerializerJQLike', '$log', 'myConstant', function ($scope, $uibModalInstance, modalItem, $http,
                                                                  $httpParamSerializerJQLike, $log, myConstant) {

        var vm = $scope.vm = {
            modalTitle: 'please change the title',
            modalBody: '#',
            url: '',
            item: {}
        }

        vm = $scope.vm = modalItem;

        vm.constant = myConstant;

        $scope.save = function () {
            $http.post(vm.url, vm.item,config.jsonHeader)
                .then(function (response) {
                    if (response.data.success) {
                        if(response.data.code == '03'){
                            alert("申请中，可再次发起申请");
                        }else if(response.data.code == '01'){
                            alert("操作成功");
                        }else{
                            alert(response.data.msg);
                        }
                        $uibModalInstance.close(vm.item);
                    } else {
                            alert(response.data.msg);
                    }
                }, function (response) {
                    alert("操作失败");
                })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    }]);